home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Hello / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  6.6 KB  |  236 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "Hello.hpp"
  11.  
  12. #ifndef CONTENT_H
  13. #include "Content.h"
  14. #endif
  15.  
  16. #ifndef PART_H
  17. #include "Part.h"
  18. #endif
  19.  
  20. #ifndef FWUTIL_H
  21. #include "FWUtil.h"
  22. #endif
  23.  
  24. #ifndef FWKIND_H
  25. #include "FWKind.h"
  26. #endif
  27.  
  28. #ifndef FWRESOUR_H
  29. #include "FWResour.h"
  30. #endif
  31.  
  32. #ifndef FWSUSINK_H
  33. #include "FWSUSink.h"
  34. #endif
  35.  
  36. #ifndef FWSUUTIL_H
  37. #include "FWSUUtil.h"
  38. #endif
  39.  
  40. #ifndef FWMEMHLP_H
  41. #include "FWMemHlp.h"
  42. #endif
  43.  
  44. #ifndef FWBUFSIN_H
  45. #include "FWBufSin.h"
  46. #endif
  47.  
  48. #ifndef SOM_ODStorageUnit_xh
  49. #include <StorageU.xh>
  50. #endif
  51.  
  52. //========================================================================================
  53. //    Runtime info
  54. //========================================================================================
  55.  
  56. #ifdef FW_BUILD_MAC
  57. #pragma segment odfhello
  58. #endif
  59.  
  60. FW_DEFINE_AUTO(CHelloContent)
  61.  
  62. //========================================================================================
  63. //    CHelloContent class
  64. //========================================================================================
  65.  
  66. //----------------------------------------------------------------------------------------
  67. // CHelloContent constructor
  68. //----------------------------------------------------------------------------------------
  69.  
  70. CHelloContent::CHelloContent(Environment* ev, CHelloPart* part) :
  71.     FW_CContent(ev, part),
  72.     fTextData(""),
  73.     fCentered(FALSE),
  74.     fHelloPart(part)
  75. {
  76.     // ----- Initialize the text data strings -----
  77.     FW_PSharedLibraryResourceFile resFile(ev);
  78.  
  79.     FW_CString32 platformString;
  80.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, FW_kMultiStringRes, kFirstString, fTextData);
  81.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, FW_kMultiStringRes, kPlatformString, platformString);
  82.     fTextData += platformString;
  83. }
  84.  
  85. //----------------------------------------------------------------------------------------
  86. // CHelloContent destructor
  87. //----------------------------------------------------------------------------------------
  88.  
  89. CHelloContent::~CHelloContent()
  90. {
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // CHelloContent::GetTextData
  95. //----------------------------------------------------------------------------------------
  96.  
  97. const FW_CString& CHelloContent::GetTextData()
  98. {
  99.     return fTextData;
  100. }
  101.  
  102. //----------------------------------------------------------------------------------------
  103. // CHelloContent::SetTextData
  104. //----------------------------------------------------------------------------------------
  105.  
  106. void CHelloContent::SetTextData(Environment* ev, const FW_CString& newText)
  107. {
  108.     fTextData = newText;
  109.     fHelloPart->PartChanged(ev, true);    // invalidateOnly = true
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. // CHelloContent::ClearTextData
  114. //----------------------------------------------------------------------------------------
  115.  
  116. void CHelloContent::ClearTextData(Environment* ev)
  117. {
  118.     FW_CString newString;
  119.     FW_PSharedLibraryResourceFile resFile(ev);
  120.     ::FW_LoadStringByID(ev, resFile, kHelloPartStrings, FW_kMultiStringRes, kBlankString, newString);
  121.  
  122.     fTextData = newString;
  123. }
  124.  
  125. //----------------------------------------------------------------------------------------
  126. // CHelloContent::ExternalizeKind
  127. //----------------------------------------------------------------------------------------
  128.  
  129. void CHelloContent::ExternalizeKind(Environment* ev,
  130.                                  ODStorageUnit* storageUnit,
  131.                                  FW_CKind* kind,
  132.                                  FW_StorageKinds storageKind,
  133.                                  FW_CPromise* promise,
  134.                                  FW_CCloneInfo* cloneInfo)
  135. {
  136.     FW_UNUSED(cloneInfo);
  137.     FW_UNUSED(storageKind);
  138.     FW_UNUSED(promise);
  139.     
  140.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  141.     FW_CWritableStream stream(suSink);
  142.  
  143.     if (kind->IsPartKind(ev)) 
  144.     {
  145.         stream << fCentered;
  146.         stream << fTextData;
  147.     }
  148.     else if (kind->IsEqual(ev, 'TEXT', kODPlatformDataType))
  149.     {
  150.         stream.Write(fTextData.RevealBuffer(), fTextData.GetByteLength());
  151.     }
  152.     else if (kind->IsEqual(ev, 'TEXT', kODPlatformFileType))
  153.     {
  154.         FW_DEBUG_MESSAGE("Should never externalize TEXT file");
  155.     }
  156.     
  157.     // ----- Clear the end of the focused value -----
  158.     FW_SUDeleteEndOfFocusedValue(ev, storageUnit);
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // ReadStringFromStream
  163. //----------------------------------------------------------------------------------------
  164.  
  165. static void ReadStringFromStream(Environment* ev, FW_CString& string, FW_CReadableStream& stream, unsigned long textSize)
  166. {
  167. FW_UNUSED(ev);
  168.     FW_CAcquireTemporaryMemory tempMemory(textSize + 1);
  169.     char* buffer = (char*)tempMemory.GetPointer();
  170.  
  171.     stream.Read(buffer, textSize);
  172.  
  173.     buffer[textSize] = 0;
  174.     string = "";
  175.     string.Append((const FW_Char*)buffer, textSize);
  176. }
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // CHelloContent::InternalizeKind
  180. //----------------------------------------------------------------------------------------
  181.  
  182. FW_Boolean CHelloContent::InternalizeKind(Environment* ev,
  183.                                      ODStorageUnit* sourceSU, 
  184.                                       FW_CKind* kind,
  185.                                      FW_StorageKinds storageKind,
  186.                                      FW_CCloneInfo* cloneInfo)
  187. {
  188.     FW_UNUSED(cloneInfo);
  189.     
  190.     unsigned long size = sourceSU->GetSize(ev);
  191.     
  192.     if (kind->IsPartKind(ev) || kind->IsEqual(ev, 'TEXT', kODPlatformDataType))
  193.     {
  194.         FW_PStorageUnitSink suSink(ev, sourceSU, kODPropContents, kind->GetType(ev));
  195.         FW_PBufferedSink sink(ev, suSink);
  196.         FW_CReadableStream stream(sink);
  197.         
  198.         if (kind->IsPartKind(ev))
  199.         {
  200.             stream >> fCentered;
  201.             stream >> fTextData;
  202.         }
  203.         else            
  204.             ReadStringFromStream(ev, fTextData, stream, sink->GetLength(ev) /*size*/);
  205.     }
  206.     else if (kind->IsEqual(ev, 'TEXT', kODPlatformFileType))
  207.     {
  208.         FW_PFileValueSink sink(ev, sourceSU);
  209.         FW_CReadableStream stream(sink);
  210.         ReadStringFromStream(ev, fTextData, stream, sink->GetLength(ev));
  211.     }
  212.     else
  213.     {
  214.         FW_DEBUG_MESSAGE("CHelloContent::InternalizeKind - Unknown type");
  215.     }
  216.         
  217.     if (storageKind != FW_kPartStorage)
  218.         fHelloPart->PartChanged(ev, true);    // invalidateOnly = true
  219.  
  220.     return TRUE;
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. // CHelloContent::CenterText
  225. //----------------------------------------------------------------------------------------
  226.  
  227. void CHelloContent::CenterText(Environment* ev, FW_Boolean state)
  228. {
  229.     if (state != fCentered)
  230.     {
  231.         fCentered = state;
  232.         fHelloPart->PartChanged(ev, false);    // invalidateOnly = false
  233.     }
  234. }
  235.  
  236.